﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class TMU02
    {
        #region ==dwFN===========
        public static readonly DependencyProperty dwFNProperty = DependencyProperty.Register("dwFN",
            typeof(DataRow), typeof(TMU02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwFNChanged));
        public DataRow dwFN
        {
            get
            {
                return (DataRow)GetValue(dwFNProperty);
            }
            set
            {
                SetValue(dwFNProperty, value);
                OnFUNCTIONChg(value);
            }
        }
        private static void OndwFNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TMU02;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwFN = row;
            }
        }
        #endregion

        public TMU02()
        {
            MV = new VICC_MENUDEF();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();             
            SResult = ((VICC_MENUDEF)MV).QueryICC_MENUDEF(dwData, 9999);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            ((VICC_MENUDEF)MV).SetUpNewRow(ref dwData);
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "MUSEQ", 1);
            dwData["ROWNID"] = LFD;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_MENUDEF)MV).UpdateICC_MENUDEF(dwMaster, app.company);
            base.SaveCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_MENUDEF)MV).DeleteICC_MENUDEF(dwMaster,2);
            string filter = "ROWNID='" + dwMaster["ROWNID"].ToString() + "'";
            DataRow[] rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {

            if (!IsNew)
            {
                USERID = "";
                IsNew = true;
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            if (USERID.Trim().Length == 0)
            {
                MessageBox.Show("请输入目标用户");
                return;
            }
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "MUSEQ", 1);
            dwData["ROWNID"] = LFD;
            dwData["USERID"] = USERID;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            USERID = "";
            base.CopyCommand(sender, e);
        }
        public void Insert(DataRow dwMU)
        {
            ((VICC_MENUDEF)MV).DeleteICC_MENUDEF(dwMU, 2);
            ((VICC_MENUDEF)MV).InsertICC_MENUDEF(dwMU, app.company);
            TBMain.Rows.InsertAt(dwMU, 0);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
        public override void OnUSERIDChg(string value)
        {
            if (dwMaster == null)
                return;
            if (!IsEdit)
                return;
            if (MV == null)
                return;
            if (USERID.Length < 8)
                return;
            dwMaster["USERID"] = value;
            base.OnUSERIDChg(value);
        }
        public override void OnFUNCChg(string value)
        {
            if (value.Length < 2)
                return;
            string sfilter = "TAG='" +value + "'";
            cvMaster.CustomFilter = sfilter;
            base.OnFUNCChg(value);
        }
        public override void OnFNCGPChg(string value)
        {
            if (dwMaster == null)
                return;
            if (!IsEdit)
                return;
            TMV.FATHER = value;
            base.OnUSERIDChg(value);
        }
        public override void OnFUNCTIONChg(DataRow value)
        {
            if (!IsEdit) return;
            if (dwMaster == null)
                return;
            if (MV == null)
                return;
            dwMaster["HEADER"] = value["DESCRIPTION"].ToString().Trim();
            dwMaster["TAG"] = value["TYPE"].ToString().Trim();
            dwMaster["MENUITEM"] = "cmd" + value["TYPE"].ToString().Trim(); 
            base.OnFUNCTIONChg(value);
        }
        private void Copy2AllUsersCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            filter = "USERID='DEMO    '";
            rows = TBMain.Select(filter);

            foreach (DataRow dwHR in cHR.TBMain.Rows)
            {
                string user = dwHR["USERID"].ToString().Trim().PadRight(8);
                if (user == "DEMO    ") continue;
                foreach (DataRow row in rows)
                {
                    dwData = TBMain.NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["USERID"] = user;
                    dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "MUSEQ", 1);
                    Insert(dwData);
                }
            }
            vwMaster.View.MoveCurrentToFirst();
            USERID = "";
        }
        private void Copy2UserCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                ART = "";
                IsNew = true; 
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            if (ART.Trim().Length == 0)
            {
                MessageBox.Show("请输入目标用户");
                return;
            }
            filter = "USERID='" + dwMaster["USERID"].ToString() + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = row.ItemArray;
                dwData["USERID"] = ART.Trim().PadRight(8);
                dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "MUSEQ", 1);
                Insert(dwData);
            }
            vwMaster.View.MoveCurrentToFirst();
            ART = "";
            IsNew = false;
        }
        private void Copy1AllUsersCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("请保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }

            filter = "USERID='" + dwMaster["USERID"].ToString() + "' and  MENUITEM='" + dwMaster["MENUITEM"].ToString() + "' ";
            rows = TBMain.Select(filter);
            foreach (DataRow dwHR in cHR.TBMain.Rows)
            {
                string user = dwHR["USERID"].ToString().Trim().PadRight(8);
                if (user == USERID) continue;
                foreach (DataRow row in rows)
                {
                    dwData = TBMain.NewRow();
                    dwData.ItemArray = dwMaster.ItemArray;
                    dwData["USERID"] = user;
                    dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "MUSEQ", 1);
                    Insert(dwData);
                }
            }
            vwMaster.View.MoveCurrentToFirst();  
        }

        private void Delete1AllUsersCommand(object sender, RoutedEventArgs e)
        {

            if (IsEdit)
            {
                USERID = "";
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            filter = "USERID<>'DEMO    ' AND MENUITEM='" + dwMaster["MENUITEM"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(row, 1);
                row.Delete();
            } 
            TBMain.AcceptChanges(); 
        }
       
    }
}